package com.park.mer.service.meruser.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.park.comm.ResultEnum;
import com.park.dao.mapper.meruser.MerUserInfoMapper;
import com.park.entity.mer.MerUserInfo;
import com.park.entity.mer.MerUserInfoDto;
import com.park.entity.mer.MerUserInfoLoginDto;
import com.park.exception.CommException;
import com.park.jwt.TokenUtils;
import com.park.mer.service.meruser.MerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @title: MerUserServiceImpl
 * @projectName park
 * @description: 商户用户业务
 * @date 2020/5/414:41
 */
@Service(value = "merUserService")
public class MerUserServiceImpl implements MerUserService {

    @Autowired
    private MerUserInfoMapper merUserInfoMapper;

    /**
     　　* @description: 商户用户登录
     　　* @param [merUserInfoLoginDto]
     　　* @return {@link MerUserInfo}
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/4 14:55
     　　*/
    public MerUserInfoDto merLogin(MerUserInfoLoginDto merUserInfoLoginDto) {
        String merPhoneNumber = merUserInfoLoginDto.getMerPhoneNumber();
        String merPassword = merUserInfoLoginDto.getMerPassword();
        //根据手机号查询账户密码
        if(StrUtil.isBlank(merPhoneNumber)){
            throw new CommException(ResultEnum.MER_USER_NAME_EMPTY_ERROR);
        }
        if(StrUtil.isBlank(merPassword)){
            throw new CommException(ResultEnum.MER_USER_PASSWORD_EMPTY_ERROR);
        }
        //根据商户手机号查询商户信息
        MerUserInfo merUserInfo = merUserInfoMapper.getByMobilePhone(merPhoneNumber);
        if(ObjectUtil.isEmpty(merUserInfo)){
            throw new CommException(ResultEnum.MER_USER_USER_EXIST_ERROR);
        }
        String merPasswordMd5 = SecureUtil.md5(merPassword);
        if(!StrUtil.equals(merPasswordMd5,merUserInfo.getMerPassword())){
            throw new CommException(ResultEnum.MER_USER_PASSWORD_ERROR);
        }
        String merToken = TokenUtils.createTokenWithClaim(merUserInfo.getId());
        MerUserInfoDto merUserInfoDto = new MerUserInfoDto();
        merUserInfoDto.setMerSignatureToken(merToken);
        merUserInfoDto.setMerUserId(merUserInfo.getId());
        return merUserInfoDto;
    }
}
